package bll

import (
	"context"
	"fmt"
	"maya-service/store/mysql"
	"maya-service/utils"

	uuid "github.com/satori/go.uuid"
	"maya-service/model"
	"maya-service/model/entity"
	"maya-service/model/mapping"
	"maya-service/store"
	"time"
)

type sysUsers struct {
	iSysUsers store.ISysUsers
}

var SysUsers = &sysUsers{
	iSysUsers: mysql.SysUsers,
}

func init() {
	Register(SysUsers)
}

func (a *sysUsers) init() func() {
	password := utils.BcryptHash("6447985")
	adminPassword := utils.BcryptHash("123456")
	// 初始化管理员
	entities := []*entity.SysUsers{
		{
			Uuid:        uuid.NewV4().String(),
			Username:    "admin",
			Password:    adminPassword,
			NickName:    "Mr.奇淼",
			HeaderImg:   "https://qmplusimg.henrongyi.top/gva_header.jpg",
			AuthorityId: 0,
			Phone:       "17611111111",
			Email:       "333333333@qq.com",
		},
		{
			Uuid:        uuid.NewV4().String(),
			Username:    "a303176530",
			Password:    password,
			NickName:    "用户1",
			HeaderImg:   "https:///qmplusimg.henrongyi.top/1572075907logo.png",
			AuthorityId: 0,
			Phone:       "17611111111",
			Email:       "333333333@qq.com"},
	}

	for i := 0; i < len(entities); i++ {
		fmt.Println(a.iSysUsers.Create(context.Background(), entities[i]))
	}
	return func() {}
}

// Create
func (a *sysUsers) Create(ctx context.Context, in *model.SysUsersCreateRequest) error {
	var (
		err error
	)
	c := buildSysUsers(in)
	_, err = a.iSysUsers.Create(ctx, c)
	return err
}

// Update
func (a *sysUsers) Update(ctx context.Context, in *model.SysUsersUpdateRequest) error {
	var (
		dict = make(map[string]interface{})
	)

	if in.Uuid != nil {
		dict["uuid"] = in.Uuid
	}

	if in.Username != nil {
		dict["username"] = in.Username
	}

	if in.Password != nil {
		dict["password"] = in.Password
	}

	if in.NickName != nil {
		dict["nick_name"] = in.NickName
	}

	if in.SideMode != nil {
		dict["side_mode"] = in.SideMode
	}

	if in.HeaderImg != nil {
		dict["header_img"] = in.HeaderImg
	}

	if in.BaseColor != nil {
		dict["base_color"] = in.BaseColor
	}

	if in.ActiveColor != nil {
		dict["active_color"] = in.ActiveColor
	}

	if in.AuthorityId != nil {
		dict["authority_id"] = in.AuthorityId
	}

	if in.Phone != nil {
		dict["phone"] = in.Phone
	}

	if in.Email != nil {
		dict["email"] = in.Email
	}

	if in.Enable != nil {
		dict["enable"] = in.Enable
	}

	if in.CreatedAt != nil {
		dict["created_at"] = in.CreatedAt
	}

	if in.UpdatedAt != nil {
		dict["updated_at"] = in.UpdatedAt
	}

	if in.DeletedAt != nil {
		dict["deleted_at"] = in.DeletedAt
	}

	// do other update here
	updateAt := time.Now().Unix()
	in.UpdatedAt = &updateAt
	return a.iSysUsers.Update(ctx, in.Id, dict)
}

// Delete
func (a *sysUsers) Delete(ctx context.Context, in *model.SysUsersDeleteRequest) error {
	return a.iSysUsers.Delete(ctx, in.Id)
}

// List
func (a *sysUsers) List(ctx context.Context, in *model.SysUsersListRequest) (*model.SysUsersListResponse, error) {
	var (
		err   error
		total int
		list  []*entity.SysUsers
		out   = &model.SysUsersListResponse{}
	)

	if total, list, err = a.iSysUsers.List(ctx, in); err != nil {
		return nil, err
	}

	out.Total = total
	out.List = mapping.SysUserssEntityToDto(list)

	return out, nil
}

// Find
func (a *sysUsers) Find(ctx context.Context, in *model.SysUsersInfoRequest) (*model.SysUsersInfo, error) {
	var (
		err  error
		data *entity.SysUsers
		out  = &model.SysUsersInfo{}
	)

	if data, err = a.iSysUsers.Find(ctx, in); err != nil {
		return nil, err
	}

	out = mapping.SysUsersEntityToDto(data)
	return out, nil
}

// buildSysUsers build entity
func buildSysUsers(in *model.SysUsersCreateRequest) *entity.SysUsers {

	ety := &entity.SysUsers{}

	ety.Uuid = in.Uuid

	ety.Username = in.Username

	ety.Password = in.Password

	ety.NickName = in.NickName

	ety.SideMode = in.SideMode

	ety.HeaderImg = in.HeaderImg

	ety.BaseColor = in.BaseColor

	ety.ActiveColor = in.ActiveColor

	ety.AuthorityId = in.AuthorityId

	ety.Phone = in.Phone

	ety.Email = in.Email

	ety.Enable = in.Enable

	ety.CreatedAt = time.Now().Unix()

	ety.UpdatedAt = in.UpdatedAt

	ety.DeletedAt = in.DeletedAt

	return ety
}
